var  {merchants} =require('../../tools/database/index.js')
var saveImg=require('../../tools/saveImg/index.js')
var PostData=require('../../tools/PostData/index.js')
module.exports=async (req,res)=>{
	console.log('接受中...')
	var data=await PostData(req)
	var userinfo=req.userinfo;
	console.log('接受完毕 等待处理')
	try{
		var product=await merchants.warehouse.getDate(userinfo._id)
		var commoditys=product.commodity;
		var img=JSON.parse(data.images);
		console.log('img',data)
		var list={
			images:[],
			title:data.title || null,
			genre:data.genre || null,
			price:data.price || null,
			sum:data.sum || null
		}
		var images=await saveImg(req,img,list.title,list.genre)
		
		console.log('图片存储完毕!')
		list.images=images;
		commoditys[data.index]=list;
		var result=await merchants.warehouse.upDate({
			"_id":userinfo._id
		},{
			"commodity":commoditys
		})
		console.log('处理完毕返回')
		res.send({
			state:200,
			msg:'修改成功'
		})
	}catch(e){
		res.send({
			state:404,
			msg:'未知错误'
		})
	}
}